import { getItineraryInfoById ,postUserItineraryAddByIdForUser} from '../../api/home'
import { imageURL } from '../../../utils/request'

Page({
  data: {
    imageURL:'',
    itinerary_id:'',
    longitude: 116.404,  // 默認北京中心坐標
    latitude: 39.915,
    itineraryDetail:{},
    markers: [{
      id: 1,
      longitude: 116.404,
      latitude: 39.915,
      iconPath: '',
      width: 30,
      height: 40,
      title: '天安門',
      address: '北京市東城區長安街'
    }],
    selectedMarker: null,
    tabs: [
    ],
    tabsIndex:'',
    days: [
      {
        title: '初識香港',
        open: true,
        day:'1',
        places: [
          {
            name: '香港科學館',
            time: '08:00–10:00 遊玩',
            text: '大片級海景打卡機位',
            image: '/pages/assets/image/motianlun.jpg'
          },
          {
            name: '香港科學館',
            time: '10:30–12:00 遊玩',
            text: '大片級海景打卡機位',
            image: '/pages/assets/image/motianlun.jpg'
          }
        ]
      },
      {
        title: '初識香港',
        open: true,
        day:'2',
        places: [
          {
            name: '香港科學館',
            time: '08:00–10:00 遊玩',
            text: '大片級海景打卡機位',
            image: '/pages/assets/image/motianlun.jpg'
          },
          {
            name: '香港科學館',
            time: '10:30–12:00 遊玩',
            text: '大片級海景打卡機位',
            image: '/pages/assets/image/motianlun.jpg'
          }
        ]
      },
      {
        title: '初識香港',
        open: true,
        day:'3',
        places: [
          {
            name: '香港科學館',
            time: '08:00–10:00 遊玩',
            text: '大片級海景打卡機位',
            image: '/pages/assets/image/motianlun.jpg'
          },
          {
            name: '香港科學館',
            time: '10:30–12:00 遊玩',
            text: '大片級海景打卡機位',
            image: '/pages/assets/image/motianlun.jpg'
          }
        ]
      },
    ]
  },
  async onLoad(e){
  this.setData({
    itinerary_id: e.itinerary_id,
    imageURL
  });
  this.getData()
  },
  async getData(){
    const res = await getItineraryInfoById({
      itinerary_id:this.data.itinerary_id
    })
    const tabs = [{ label: '總', progress: 0 }];
    const numberDay = res.data.info.number_day || 0;
    for (let i = 1; i <= numberDay; i++) {
      tabs.push({ label: String(i), progress: 0 });
    }
    // 给 list_item 每一项加上 open:true
    const list_item = (res.data.list_item || []).map(item => ({
      ...item,
      open: true
    }));
    this.setData({
      itineraryDetail: {
        ...res.data,
        list_item
      },
      tabs
    });
  },
  toggleDay(e) {
    const index = e.currentTarget.dataset.index;
    const days = this.data.itineraryDetail.list_item;
    days[index].open = !days[index].open;
    this.setData({ 
      'itineraryDetail.list_item':days
     });
  },
  async addClick(){
    const {itineraryDetail} = this.data
    
    const res = await postUserItineraryAddByIdForUser({
      itinerary_id:itineraryDetail.info.itinerary_id
    })
    wx.showToast({
      title: '添加成功',
    })
    setTimeout(()=>{
      wx.navigateBack()
    },500)
  },
  onAddTab() {
    const { tabs } = this.data;
    const newTab = { label: tabs.length, progress: 0 };
    tabs.push(newTab);
    this.setData({
      tabs
    })
  },

  onTabClick(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      tabsIndex:index
    })
  },

  onPreviewImage(e) {
    const index = e.currentTarget.dataset.index;
    wx.previewImage({
      current: this.data.carouselImages[index],
      urls: this.data.carouselImages
    });
  },
  // 地圖區域變化
  onRegionChange(e) {
    console.log('地圖移動', e);
  },

  // 回到當前位置
  moveToLocation() {
    wx.navigateTo({
      url: `/pages/Itinerary/bigMap/bigMap?itinerary_id=${this.data.itinerary_id}&type=hot`,
    })
  },
});
